<?php 
/*
 module:		节点管理控制器
 create_time:	2022-08-21 10:24:42
 author:		
 contact:		
*/

namespace app\Controller\admin;
use function Hyperf\ViewEngine\view;
use App\Exception\ValidateException;
use App\Model\admin\NodeModel;
use App\Controller\admin\CommonController;
use Hyperf\DbConnection\Db;

class NodeController extends CommonController {


	/*
 	* @Description  数据列表
 	*/
	function index(){
		if($this->request->isMethod('get')){
			return view('admin/node/index');
		}else{
			$limit  = $this->request->input('limit', 20);
			$page = $this->request->input('page', 1);

			$order  = $this->request->input('order');	//排序方式
			$sort  = $this->request->input('sort');		//排序字段

			$where = [];
			$where['node_id'] = $this->request->input('node_id');

			$sort = !empty($sort) ? $sort : 'sortid';
			$order = !empty($order) ? $order : 'asc';

			$field = 'node_id,title,type,path,status,icon,sortid,pid';

			$query = NodeModel::select(explode(",",$field));

			$res = formatWhere($query,$where)->orderBy($sort,$order)->paginate($limit)->toArray();

			$res['data'] = _generateListTree($res['data'],0,['node_id','pid']);

			$data['status'] = 200;
			$data['data'] = $res;
			$page == 1 && $data['sql_field_data'] = $this->getSqlField('pid');
			return json($data);
		}
	}


	/*
 	* @Description  修改排序开关
 	*/
	function updateExt(){
		$postField = 'node_id,status,sortid';
		$data = $this->request->inputs(explode(',',$postField),'post');
		if(!$data['node_id']) throw new ValidateException ('参数错误');
		
		$data = array_filter($data);
		
		NodeModel::whereIn('node_id',explode(',',$data['node_id']))->update($data);
		return json(['status'=>200,'msg'=>'操作成功']);
	}

	/*
 	* @Description  添加
 	*/
	public function add(){
		$postField = 'pid,title,type,path,status,icon,sortid';
		$data = $this->request->inputs(explode(',',$postField),'post');

		$validate = new \App\Validate\admin\NodeValidate;
		if(!$validate->scene('add')->check($data)){
			throw new ValidateException ($validate->getError());
		}

		try{
			$res = NodeModel::insertGetId($data);
			if($res && empty($data['sortid'])){
				NodeModel::where('node_id',$res)->update(['sortid'=>$res]);
			}
		}catch(\Exception $e){
			throw new ValidateException($e->getMessage());
		}
		return json(['status'=>200,'data'=>$res,'msg'=>'添加成功']);
	}


	/*
 	* @Description  修改
 	*/
	public function update(){
		$postField = 'node_id,pid,title,type,path,status,icon,sortid';
		$data = $this->request->inputs(explode(',',$postField),'post');

		$validate = new \App\Validate\admin\NodeValidate;
		if(!$validate->scene('update')->check($data)){
			throw new ValidateException ($validate->getError());
		}


		if(!isset($data['pid'])){
			$data['pid'] = null;
		}

		try{
			NodeModel::where('node_id',$data['node_id'])->update($data);
		}catch(\Exception $e){
			throw new ValidateException($e->getMessage());
		}
		return json(['status'=>200,'msg'=>'修改成功']);
	}


	/*
 	* @Description  修改信息之前查询信息的 勿要删除
 	*/
	function getUpdateInfo(){
		$id =  $this->request->input('node_id');
		if(!$id) throw new ValidateException ('参数错误');
		$field = 'node_id,pid,title,type,path,status,icon,sortid';
		$res = NodeModel::select(explode(",",$field))->find($id);
		return json(['status'=>200,'data'=>$res]);
	}


	/*
 	* @Description  删除
 	*/
	function delete(){
		$idx =  $this->request->input('node_id');
		if(!$idx) throw new ValidateException ('参数错误');
		NodeModel::whereIn('node_id',explode(',',$idx))->delete();
		return json(['status'=>200,'msg'=>'操作成功']);
	}


	/*
 	* @Description  查看详情
 	*/
	function detail(){
		$id =  $this->request->input('node_id');
		if(!$id) throw new ValidateException ('参数错误');

		$field = 'node_id,title,type,path,status,icon,sortid';

		$res = NodeModel::select(explode(",",$field))->find($id);
		return json(['status'=>200,'data'=>$res]);
	}


	/*
 	* @Description  获取定义sql语句的字段信息
 	*/
	function getFieldList(){
		return json(['status'=>200,'data'=>$this->getSqlField('pid')]);
	}

	/*
 	* @Description  获取定义sql语句的字段信息
 	*/
	private function getSqlField($list){
		$data = [];
		if(in_array('pid',explode(',',$list))){
			$data['pids'] = _generateSelectTree($this->query('select node_id,title,pid from pre_node order by sortid asc','default'));
		}
		return $data;
	}





}



